package com.example.servicetest.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.text.TextPaint
import android.util.AttributeSet
import android.view.View
import androidx.core.view.marginBottom
import androidx.core.view.marginEnd
import androidx.core.view.marginStart
import androidx.core.view.marginTop
import kotlin.math.abs

class CustomTextView : View {

    //画笔
    val paint = Paint()

    val mTextPaint = TextPaint()

    //用于测量文字宽度使用
    val mRect = Rect()

    var mText:String = "99+"

    var mTextSize:Float = 50f

    var mStrokeWidth:Float = 4f

    var mRadius = 30f

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        mTextPaint.textSize = mTextSize
        mTextPaint.color = Color.WHITE
        mTextPaint.style = Paint.Style.FILL
        mTextPaint.isAntiAlias = true
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        mTextPaint.getTextBounds(mText, 0, mText.length, mRect)
        val newWith = (mRect.width() + paddingStart + paddingEnd + marginStart + marginEnd + mStrokeWidth*2).toInt()
        val newHeight = (mRect.height() + paddingTop + paddingBottom + marginTop + marginBottom + mStrokeWidth*2).toInt()

        val sh = mRect.height()
        val sw = mRect.width()
        val newWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWith,MeasureSpec.EXACTLY)
        val newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight,MeasureSpec.EXACTLY)
        super.onMeasure(newWidthMeasureSpec, newHeightMeasureSpec)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        paint.run {
            color = Color.RED
            style = Paint.Style.FILL
            strokeWidth = mStrokeWidth
        }
        canvas.drawRoundRect(
            mStrokeWidth,
            mStrokeWidth,
            width - mStrokeWidth,
            height - mStrokeWidth,
            mRadius,
            mRadius,
            paint
        )

        paint.run {
            color = Color.WHITE
            style = Paint.Style.STROKE
            strokeWidth = mStrokeWidth
        }
        canvas.drawRoundRect(
            mStrokeWidth/2 ,
            mStrokeWidth/2 ,
            width - mStrokeWidth/2,
            height - mStrokeWidth/2,
            mRadius,
            mRadius,
            paint
        )

        mTextPaint.run {
            color = Color.WHITE
            style = Paint.Style.FILL
            isAntiAlias = true
            textSize = mTextSize
        }
        val fm: Paint.FontMetricsInt = paint.fontMetricsInt

        val y = height - (height - mRect.height())/2f - fm.descent
        val x = width/2f - mRect.width()/2f

        canvas.drawText(mText,x,y,mTextPaint)
    }

    fun setText(numberStr:String){
        mText = numberStr
        requestLayout()
    }

}